package com.belarus.kamianiec.photo.photoSite.dao;

import java.util.List;

import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.belarus.kamianiec.photo.photoSite.domain.Services;

@Repository
public class ServicesDAOImpl implements ServicesDAO {

	private SessionFactory sessionFactory;

	public void addService(Services auto) {
		String sql = "";
		// String sql = "SELECT * FROM SERVICE WHERE rnumber = '" +
		// auto.getRnumber() + "'";
		List<Services> answer = sessionFactory.getCurrentSession().createSQLQuery(sql).addEntity(Services.class).list();
		if (answer.isEmpty()) {
			sessionFactory.getCurrentSession().save(auto);
		}
	}

	@SuppressWarnings("unchecked")
	public List<Services> listServices() {
		return sessionFactory.getCurrentSession().createQuery("from Service").list();
	}

	public void removeService(Integer id) {
		Services service = (Services) sessionFactory.getCurrentSession().load(Services.class, id);
		if (null != service) {
			sessionFactory.getCurrentSession().delete(service);
		}
	}

	public SessionFactory getSessionFactory() {
		return sessionFactory;
	}

	@Autowired(required=false)
	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}

}
